//
//  JDDeleteAccountViewController.swift
//  DragonWallet
//
//  Created by volantPig on 2024/5/18.
//

import UIKit
/// 注销账号
class JDDeleteAccountViewController: JDBaseViewController {

    var mainContainerView: UIView?
    
    var bottomContainerView: UIView?
    var cancelButton: UIButton?
    var confirmButton: UIButton?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.setupConfig()
        self.setupUIElements()
        // Do any additional setup after loading the view.
    }
    
    func setupConfig() {
        self.title = "注销账号"
        self.setupLeftBackItem()
    }
    
    func setupUIElements() {
        self.bottomContainerView = UIView()
        self.view.addSubview(self.bottomContainerView!)
        self.bottomContainerView?.snp.makeConstraints({ make in
            make.left.right.equalTo(self.view)
            make.bottom.equalTo(self.view)
            make.height.equalTo(104)
        })
        self.bottomContainerView?.backgroundColor = .white

        self.cancelButton = UIButton(type: .custom)
        self.bottomContainerView?.addSubview(self.cancelButton!)
        self.cancelButton?.snp.makeConstraints({ make in
            make.right.equalTo(self.bottomContainerView!.snp.centerX).offset(-5)
            make.centerY.equalTo(self.bottomContainerView!)
            make.left.equalTo(self.bottomContainerView!).offset(12)
            make.height.equalTo(54)
        })
        self.cancelButton?.layer.cornerRadius = 27
        self.cancelButton?.backgroundColor = color("#F5F6F8")
        self.cancelButton?.setTitle("返回", for: .normal)
        self.cancelButton?.titleLabel?.font = UIFont.systemFont(ofSize: 16, weight: .regular)
        self.cancelButton?.setTitleColor(.black, for: .normal)
        self.cancelButton?.addTarget(self, action: #selector(backAction), for: .touchUpInside)
        
        self.confirmButton = UIButton(type: .custom)
        self.bottomContainerView?.addSubview(self.confirmButton!)
        self.confirmButton?.snp.makeConstraints({ make in
            make.left.equalTo(self.bottomContainerView!.snp.centerX).offset(5)
            make.right.equalTo(self.bottomContainerView!).offset(-12)
            make.height.equalTo(54)
            make.centerY.equalTo(self.bottomContainerView!)
        })
        self.confirmButton?.setTitle("确定注销", for: .normal)
        self.confirmButton?.backgroundColor = color("#FA6161")
        self.confirmButton?.titleLabel?.font = UIFont.systemFont(ofSize: 16, weight: .regular)
        self.confirmButton?.setTitleColor(.white, for: .normal)
        self.confirmButton?.layer.cornerRadius = 27
        self.confirmButton?.addTarget(self, action: #selector(deleteAccountAction), for: .touchUpInside)
        
        self.mainContainerView = UIView()
        self.view.addSubview(self.mainContainerView!)
        self.mainContainerView?.snp.makeConstraints({ make in
            make.left.equalTo(self.view).offset(12)
            make.right.equalTo(self.view).offset(-12)
            make.top.equalTo(self.view).offset(14+statusBarHeight+44)
            make.bottom.equalTo(self.bottomContainerView!.snp.top).offset(-12)
        })
        self.mainContainerView?.layer.cornerRadius = 9
        self.mainContainerView?.backgroundColor = .white
        
        let titleL = UILabel()
        self.mainContainerView?.addSubview(titleL)
        titleL.snp.makeConstraints { make in
            make.left.equalTo(self.mainContainerView!).offset(16)
            make.top.equalTo(self.mainContainerView!).offset(16)
            make.right.equalTo(self.mainContainerView!).offset(-16)
        }
        titleL.text = "注销当前账号"
        titleL.labelConfig(textAlignment: .left, font: UIFont.systemFont(ofSize: 20, weight: .semibold), textColor: color("#000143"))
        
        let content1 = UILabel()
        self.mainContainerView?.addSubview(content1)
        content1.snp.makeConstraints { make in
            make.left.right.equalTo(titleL)
            make.top.equalTo(titleL.snp.bottom).offset(27)
        }
        content1.text = "重要注销账号为不可恢复操作，注销账号后将无法找回本账号，及本账号相关的内容与信息;"
        content1.labelConfig(textAlignment: .left, font: UIFont.systemFont(ofSize: 16, weight: .regular), textColor: color("#000143"), numberOfLines: 0)
        
        let content2 = UILabel()
        self.mainContainerView?.addSubview(content2)
        content2.snp.makeConstraints { make in
            make.left.right.equalTo(titleL)
            make.top.equalTo(content1.snp.bottom).offset(27)
        }
        content2.text = "在您注销账号之前，请您认真阅读，理解并同意以下内容:"
        content2.labelConfig(textAlignment: .left, font: UIFont.systemFont(ofSize: 16, weight: .regular), textColor: color("#000143"), numberOfLines: 0)
        
        let contentLabel = UILabel()
        self.mainContainerView?.addSubview(contentLabel)
        contentLabel.snp.makeConstraints { make in
            make.left.right.equalTo(titleL)
            make.top.equalTo(content2.snp.bottom).offset(27)
        }
        contentLabel.text = "1.注销账号需满足以下条件:\n没有资产、欠款、未结清的资金和虚拟权益。账号状态正常\n2.注销账号后您将无法找回本账号，及账号相关的任何内容与信息，包括但不限于。\n·账号内的个人资料，浏览、收藏、评论、关注等操作的内容;\n·账号内产生的订单、余额、虚拟权益等!\n3.注销账号并不代表注销前的账号中的行为相关责任得到豁免或减轻。"
        contentLabel.labelConfig(textAlignment: .left, font: UIFont.systemFont(ofSize: 16, weight: .semibold), textColor: color("#000143"), numberOfLines: 0)
    }
    
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

    @objc func deleteAccountAction() {
        printLog("确定注销账号")
        
        JDNetWorkRequest(.deleteAccount([:])) { responseStr in
            
            let commonModel = JDCommonModel.deserialize(from: responseStr, designatedPath: "")
            if commonModel?.code == "0" {
                // 成功
                JDLoginModel.default.userLogout()
                UserDefaults.standard.removeObject(forKey: "loginInfo")
                self.navigationController?.popViewController(animated: true)
                
            } else if commonModel?.code == "401" {
                // 未登录
            }
            
        } failure: { error in
            
        } error: { errorCode in
            
        }

    }
    
}
